package com.boshao.classschedule.fragment;

import java.util.Calendar;
import java.util.Map;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.boshao.classschedule.ClassApplication;
import com.boshao.classschedule.R;
import com.boshao.classschedule.adapter.ClassGridAdapter;
import com.boshao.classschedule.utils.TimeUtils;

/**
 * 线程课程表Fragment
 * @author boshao
 *
 */
public class ClassScheduleFragment extends Fragment{
	private View mRootView;
	private int mCurrentYear;
	private int mCurrentMonth;
	private int mCurrentDay;
	private String mCurrentDate;
	private Map<Integer,String> mMapDaysOfWeek;
	private LinearLayout mllWeekHeader;
	private TextView mTvHeader;
	private static TextView mNumOfWeek;
	private static int mCurrentWeek;
	private View mWeeklayout;
	public static final String[] WEEKDAYS = {"周一","周二","周三","周四","周五","周六","周日"};
	private static ClassGridAdapter sClassGridAdapter;
	
	public static ClassScheduleFragment newInstance(){
		ClassScheduleFragment classFragment = new ClassScheduleFragment();
		return classFragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if(mRootView == null){
			mRootView = inflater.inflate(R.layout.fragment_class,container,false);
			mllWeekHeader = (LinearLayout) mRootView.findViewById(R.id.ll_week_text);
			initData();
			setWeekText(inflater,mMapDaysOfWeek);
			RecyclerView rv =  (RecyclerView) mRootView.findViewById(R.id.gv_schedule);
			LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
			rv.setLayoutManager(layoutManager);
			sClassGridAdapter = new ClassGridAdapter(getActivity(),this);
			rv.setAdapter(sClassGridAdapter);		
			mTvHeader = (TextView) mRootView.findViewById(R.id.tv_date_header);
			mTvHeader.setText(String.format("%d年春",mCurrentYear));
			mNumOfWeek = (TextView) mRootView.findViewById(R.id.tv_class_schedule_week_num);
			mCurrentWeek = TimeUtils.getNumberOfWeeks(mCurrentDate);
			mNumOfWeek.setText(String.format("第%d周",mCurrentWeek));
			mNumOfWeek.setOnClickListener(new ClassOnclickListener());
		}else{
			((ViewGroup)mRootView.getParent()).removeView(mRootView);
		}
		return mRootView;
	}

	private void setWeekText(LayoutInflater inflater,Map<Integer,String> mapDaysOfWeek) {
		mllWeekHeader.removeAllViews();
		for (int i = 0; i < 7; i++) {
			mWeeklayout = inflater.inflate(R.layout.class_schedule_header,null);
			mllWeekHeader.addView(mWeeklayout);
			LinearLayout.LayoutParams params = (LayoutParams) mWeeklayout.getLayoutParams();
			if(i != 6){
				params.width = (ClassApplication.getInstance().getDisplayWidth() - 
						getResources().getDimensionPixelSize(R.dimen.tv_schedule_week_left_width))/7;
			}else{
				int each = ( ClassApplication.getInstance().getDisplayWidth() - 
				getResources().getDimensionPixelSize(R.dimen.tv_schedule_week_left_width) )/7;
				params.width = ClassApplication.getInstance().getDisplayWidth() - 
						getResources().getDimensionPixelSize(R.dimen.tv_schedule_week_left_width) - each * 6;
			}
			TextView dayOfMonth = (TextView) mWeeklayout.findViewById(R.id.tv_schedule_date);
			TextView dayOfWeek = (TextView) mWeeklayout.findViewById(R.id.tv_schedule_week_day);
			dayOfMonth.setText(mapDaysOfWeek.get(i).substring(8));
			dayOfWeek.setText(WEEKDAYS[i]);
		}
	}

	private void initData() {
		mCurrentYear = TimeUtils.getCurrentYear();
		mCurrentMonth = TimeUtils.getCurrentMonth();
		mCurrentDay = TimeUtils.getCurrentDay();
		mCurrentDate = TimeUtils.getFormatDate(mCurrentYear, mCurrentMonth,mCurrentDay);
		mMapDaysOfWeek = getWeekDays2Map(mCurrentDate);
	}
			
	class ClassOnscrollListener implements OnScrollListener{

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			
		}
		
	}
	
	public TextView getWeekTextView(){
		return mNumOfWeek;
	}
	
	public void changeWeeksHeaderView(int week){
		if(week > mCurrentWeek){
			int f = week - mCurrentWeek;
			Calendar curCalendar = TimeUtils.getCalendarOfDate(mCurrentDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(curCalendar.getTimeInMillis() + 7*f*TimeUtils.getOneDayMilliseconds());			
			String date = TimeUtils.getFormatDate(calendar.get(Calendar.YEAR),
					calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
			setWeekText(LayoutInflater.from(getActivity()),getWeekDays2Map(date));
		}else if(week < mCurrentWeek){
			int f = mCurrentWeek - week;
			Calendar curCalendar = TimeUtils.getCalendarOfDate(mCurrentDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(curCalendar.getTimeInMillis() - 7*f*TimeUtils.getOneDayMilliseconds());			
			String date = TimeUtils.getFormatDate(calendar.get(Calendar.YEAR),
					calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
			setWeekText(LayoutInflater.from(getActivity()),getWeekDays2Map(date));
		}else{
			setWeekText(LayoutInflater.from(getActivity()), mMapDaysOfWeek);
		}
	}

	private Map<Integer,String> getWeekDays2Map(String date) {
		Map<Integer,String> map = null;
		if(TimeUtils.getWeekDay(date) == 0){
			Calendar currentCalendar = TimeUtils.getCalendarOfDate(date);
			Calendar yesterdayCalendar = Calendar.getInstance();
			yesterdayCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() - TimeUtils.getOneDayMilliseconds());
			map = TimeUtils.getCurrentWeekDays(TimeUtils.getFormatDate(yesterdayCalendar.get(Calendar.YEAR),yesterdayCalendar.get(Calendar.MONTH)+1
					,yesterdayCalendar.get(Calendar.DAY_OF_MONTH)));
		}else{
			map = TimeUtils.getCurrentWeekDays(date);
		}
		return map;
	}
				
	class ClassOnclickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv_class_schedule_week_num:
				SelectWeekDialogFragment.newInstance(ClassScheduleFragment.this)
					.show(getFragmentManager(),SelectWeekDialogFragment.TAG);
				break;
			}
		}
	}
	
	/**
	 * 更新课程表UI
	 */
	public void refrashView(){
		sClassGridAdapter.setCurrentWeek(Integer.parseInt(mNumOfWeek.getText().toString().substring(1,2)));
		sClassGridAdapter.notifyDataSetChanged();
		for(int i = 0;i < sClassGridAdapter.mViewHolders.size();i++){
			sClassGridAdapter.mViewHolders.get(i).mScheduleGridAdapter.notifyDataSetChanged();
		}
	}
		
}
